<template>
  <div class="header">
    <div class="container">
      <div class="left-box">
        <div @click="router.push('/')" class="logo">
          <img src="../../assets/image/logo.png" alt="" />
        </div>
        <div class="nav">
          <nav class="nav-list">
            <div
              :class="{ active: isActive(data.targetPath) }"
              class="nav-item"
              v-for="data in navList"
              :key="1"
            >
              <router-link :to="data.targetPath">{{ data.title }}</router-link>
            </div>
          </nav>
        </div>
        <div class="search">
          <!-- <searchInput @search="searchMatch"></searchInput> -->
        </div>
      </div>
      <div class="right-user">
        <div class="post example-showcas">
          <el-dropdown :teleported="true">
            <el-button style="width: 100px; text-align: center">
              <span style="text-align: center">发布</span></el-button
            >
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item
                  @click="$router.push('/publish-contest')"
                  command="a"
                  >发布竞赛</el-dropdown-item
                >
                <el-dropdown-item
                  @click="$router.push('/publish-events')"
                  command="b"
                  >发布活动</el-dropdown-item
                >
                <el-dropdown-item
                  @click="$router.push('/addOrUpdataArticle')"
                  command="b"
                  >发布文章</el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>

          <el-badge
            v-if="isLogin"
            style="margin: 0 20px"
            :value="notReadCount"
            class="item"
          >
            <el-icon
              @click="routeMessage"
              color="#333"
              size="20"
              style="margin: 0 10px; margin-top: 8px; cursor: pointer"
              class="no-inherit"
            >
              <Message />
            </el-icon>
          </el-badge>

        </div>
        <div
          v-if="!isLogin"
          class="reg-and-login"
        >
          <div class="reg" 
          @click="$router.push({ path: '/login', query: { isReg: true}})">
            <a>注册</a>
          </div>
          <p class="margin">/</p>
          <div class="login"
          @click="$router.push({ path: '/login', query: { isReg: false}})">
            <a>登录</a>
          </div>
        </div>
        <el-dropdown v-else :teleported="true">
          <div
            @click="gotoMyPage"
            style="
              text-align: center;
              cursor: pointer;
              border-radius: 50%;
              border: none;
            "
            class="user-info"
          >
            <el-avatar :size="40" :src="userStore.userInfo.data.avatar" />
            <p>{{ userStore.userInfo.data.nickName }}</p>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="$router.push('/my')" command="a"
                >个人中心</el-dropdown-item
              >
              <el-dropdown-item @click="userStore.logout()" command="b"
                >退出登录</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// import searchInput from "./Search-input.vue";
import { computed, ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import useUserStore from "@/stores/user";
import { getHomeMessageNotReadApi } from "@/api/user";

const route = useRoute();
const router = useRouter();
let notReadCount = ref(0);
const userStore = useUserStore();

const isLogin = computed(() => {
  return !!userStore.userInfo.token;
});
onMounted(async () => {
  //注意这个地方需要先登录才去调用这个接口
  if (!!userStore.userInfo.token) {
    // 获取首页邮件数量数据
    const res = await getHomeMessageNotReadApi();
    notReadCount.value = res.data.data;
  }
});

const isActive = (path: string) => {
  return path === route.path;
};

interface NavList {
  title: string;
  targetPath: string;
}

function gotoMyPage() {
  router.push("/my");
}

/**
 * 点击跳转消息中心
 */
function routeMessage() {
  router.push("/message");
}

const navList: NavList[] = [
  {
    title: "首页",
    targetPath: "/index",
  },
  {
    title: "竞赛",
    targetPath: "/contests",
  },
  {
    title: "活动",
    targetPath: "/events",
  },
  // {
  //   title: "课程",
  //   targetPath: "/course",
  // },
  {
    title: "文章",
    targetPath: "/article",
  },
  // {
  //   title: "题库",
  //   targetPath: "/q-bank",
  // },
  // {
  //   title: "图书",
  //   targetPath: "/book",
  // },
  // {
  //   title: "实习",
  //   targetPath: "/practice",
  // },
];

</script>

<style lang="scss" scoped>
.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}

.header {
  position: sticky;
  top: 0;
  z-index: 1000;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  background: #fff;

  .container {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .left-box {
      flex-basis: 78%;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .logo {
        width: 108px;
        height: fit-content;

        box-sizing: border-box img {
          width: 100px;
        }
      }

      .nav {
        flex-basis: 55%;

        .nav-list {
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: space-between;

          > .nav-item {
            margin: 0 10px;
            font-size: 16px;
            padding-bottom: 5px;
            color: rgb(102, 102, 102);

            &.active {
              border-bottom: 3px solid $main-color;
            }
          }
        }
      }

      .search {
        .search-input :deep(.el-input) {
          background: #f3f5f6;
        }
      }
    }

    .right-user {
      flex-basis: 22%;
      display: flex;
      align-items: center;
      justify-content: flex-end;

      > div {
        margin: 0 10px;
      }

      .post {
        :deep(.el-button) {
          padding: 5px 13px;
          width: 80px;
          height: 34px;
          border: 1px solid $main-color;
          color: $main-color;

          &:hover {
            background: $main-color;
            color: #fff;
          }

          span {
            width: 60px;
            justify-content: space-between;
          }
        }
      }

      .reg-and-login {
        display: flex;
        align-items: center;
        justify-content: space-between;
        cursor: pointer;

        

        .reg,
        .login {
          font-size: 14px;
          transition: color 0.1s ease-in-out;

          &:hover {
            color: $main-color;
          }
        }

        .margin {
          font-size: 20px;
          color: #d1d1d1;
          margin: 0 10px;
        }
      }
    }
  }
}
</style>
